/**
  ******************************************************************************
  * @file    user_freertos.c
  * @version V1.0.0.180116_Beta
  * @date    2018.01.16
  * @author  bigvan_tech  
  * @brief   Source code for freertos applications.
  ******************************************************************************
  * @note
  *
  * E-mail：bigvan_tech@126.com
  * Change Logs:
  * ----------------------------------------------------------------
  * Date        Version                 Author         Description
  * --------    --------------------    -----------    -------------
  * 20180116    V1.0.0.180116_Beta     	bigvan_tech    Tested.
  *
  ******************************************************************************  
  */

/* Includes ------------------------------------------------------------------*/
#include "user_freertos.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
TaskHandle_t xTask0AHandle = NULL;
TaskHandle_t xTask0BHandle = NULL;
TaskHandle_t xTask2AHandle = NULL;
TaskHandle_t xTask2BHandle = NULL;

/* Private function prototypes -----------------------------------------------*/
void vTask0A(void *pvParameters);
void vTask0B(void *pvParameters);
void vTask1(void *pvParameters);
void vTask2A(void *pvParameters);
void vTask2B(void *pvParameters);

/* Exported functions --------------------------------------------------------*/
/**
  * @brief  Create tasks.
  * @param  None
  * @retval None
  */
void AppTaskCreate(void)
{
	/* Create Task0A at priority 2. */
	xTaskCreate(vTask0A,
							"Task0A",
							128,
							NULL,
							2,
							&xTask0AHandle);
	
	/* Create Task0B at priority 2. */
	xTaskCreate(vTask0B,
							"Task0B",
							128,
							NULL,
							2,
							&xTask0BHandle);
	
	/* Create Task1 at priority 3. */
	xTaskCreate(vTask1,
							"Task1",
							128,
							NULL,
							3,
							NULL);
		
	/* Create Task2A at priority 4. */
	xTaskCreate(vTask2A,
							"Task2A",
							128,
							NULL,
							4,
							&xTask2AHandle);
							
	/* Create Task2B at priority 4. */
	xTaskCreate(vTask2B,
							"Task2B",
							128,
							NULL,
							4,
							&xTask2BHandle);
}

/* Private functions ---------------------------------------------------------*/
/**
  * @brief  The implementation of Task0A. 
  * @param  pvParameters: Task functions accept a parameter of type void*.
  * @retval None
  */
void vTask0A(void *pvParameters)
{
	for ( ;; )
	{
		/* Print out the name of this task. */
		vTaskSuspend(xTask0AHandle);
	}
}

/**
  * @brief  The implementation of Task0B. 
  * @param  pvParameters: Task functions accept a parameter of type void*.
  * @retval None
  */
void vTask0B(void *pvParameters)
{
	for ( ;; )
	{				
		vTaskSuspend(xTask0BHandle);
	}
}

/**
  * @brief  The implementation of Task1. 
  * @param  pvParameters: Task functions accept a parameter of type void*.
  * @retval None
  */
void vTask1(void *pvParameters)
{	
	for ( ;; )
	{	
		/* Toggle LED1 */
		HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
		
		/* Create task0 at a lower priority. */
		vTaskResume(xTask0AHandle);
		
		/* Create task2 at a higher priority. */
		vTaskResume(xTask2AHandle);
		
		vTaskDelay(pdMS_TO_TICKS(500));
	}
}

/**
  * @brief  The implementation of Task2A.  
  * @param  pvParameters: Task functions accept a parameter of type void*.
  * @retval None
  */
void vTask2A(void *pvParameters)
{
	for ( ;; )
	{			
		vTaskSuspend(xTask2AHandle);
	}
}

/**
  * @brief  The implementation of Task2B.  
  * @param  pvParameters: Task functions accept a parameter of type void*.
  * @retval None
  */
void vTask2B(void *pvParameters)
{
	for ( ;; )
	{			
		vTaskSuspend(xTask2BHandle);
	}
}

/**
  * @brief EXTI line detection callbacks
  * @param GPIO_Pin: Specifies the pins connected EXTI line
  * @retval None
  */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
	BaseType_t xYieldRequired;
	
  if(GPIO_Pin == KEY0_Pin)
  {		
		/* Resume the suspended Task0B. */
		xYieldRequired = xTaskResumeFromISR(xTask0BHandle);
		
		if(xYieldRequired == pdTRUE)
		{
			/* Toggle LED0 */
			HAL_GPIO_TogglePin(LED0_GPIO_Port, LED0_Pin);	
		}
  }  
  else if(GPIO_Pin == KEY2_Pin)
  {
		/* Resume the suspended Task2B. */
		xYieldRequired = xTaskResumeFromISR(xTask2BHandle);
		
		{
			/* Toggle LED2 */
			HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin);		
		}
  }	
}

/********************************END OF FILE***********************************/
